name: update-saas-frontend

# Edit with caution ! Made for build+test on "feature/*" push and pull requests events
# And for only deploying on pushes on "main" branch
on:
  push:
    branches: [main, develop, "feature/*"]
    paths:
      - "twake/frontend/**"
      - "twake/docker/**"
      - ".github/workflows/**"
  pull_request:
    branches: ["feature/*"]
    paths:
      - "twake/frontend/**"

jobs:
  build-frontend:
    runs-on: ubuntu-20.04
    env:
      FRONTEND_ENV: ${{secrets.FRONTEND_ENV}}

    strategy:
      matrix:
        node-version: [12.x]

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm install -g yarn
      - run: cd twake/frontend/ && yarn install
      - run: cd twake/frontend/ && cp src/app/environment/environment.ts.dist src/app/environment/environment.ts
      - run: cp twake/frontend/src/app/environment/environment.ts.dist twake/frontend/src/app/environment/environment.ts
      - if: ${{env.FRONTEND_ENV}}
        run: echo ${{ secrets.FRONTEND_ENV }} > twake/frontend/src/app/environment/environment.ts
      - run: cd twake/frontend/ && yarn build
      - name: Upload frontend build artifact
        uses: actions/upload-artifact@v1
        with:
          name: frontend-build
          path: twake/frontend/build/

  deploy-nginx:
    needs: build-frontend
    runs-on: ubuntu-20.04
    if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
    steps:
      - name: Set env to develop
        if: endsWith(github.ref, '/develop')
        run: 'echo "DOCKERTAG=develop" >> $GITHUB_ENV'
      - name: Set env to production
        if: endsWith(github.ref, '/main')
        run: 'echo "DOCKERTAG=latest" >> $GITHUB_ENV'
      - uses: actions/checkout@v2
      - name: Publish to Registry
        uses: elgohr/Publish-Docker-Github-Action@master
        with:
          name: twaketech/twake-nginx
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          workdir: twake
          buildoptions: "-t twaketech/twake-nginx -f docker/twake-nginx/Dockerfile"
          tags: "${{ env.DOCKERTAG }}"

  #TODO tests frontend

  # The nexts parts will work only on main push

  deploy-saas:
    needs: deploy-nginx
    runs-on: ubuntu-20.04
    if: github.ref == 'refs/heads/main'

    steps:
      - uses: actions/checkout@v2

      - name: Download frontend-build artifact
        uses: actions/download-artifact@v1
        with:
          name: frontend-build

      - uses: jakejarvis/s3-sync-action@v0.5.1
        with:
          args: --acl public-read --follow-symlinks --delete
        env:
          AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_REGION: "eu-west-3" # optional: defaults to us-east-1
          SOURCE_DIR: "frontend-build" # optional: defaults to entire repository

      - uses: awact/cloudfront-action@0.1.2
        env:
          SOURCE_PATH: "/*"
          AWS_REGION: "us-east-1"
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          DISTRIBUTION_ID: ${{ secrets.DISTRIBUTION_ID }}
